package org.jeecg.modules.wlhy.invoiceinfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.invoiceinfo.entity.HyInvoiceInfo;
import org.jeecg.modules.wlhy.invoiceinfo.service.IHyInvoiceInfoService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

 /**
 * @Description: 开票信息管理
 * @Author: jeecg-boot
 * @Date:   2021-05-24
 * @Version: V1.0
 */
@Api(tags="开票信息管理")
@RestController
@RequestMapping("/invoiceinfo/hyInvoiceInfo")
@Slf4j
public class HyInvoiceInfoController extends JeecgController<HyInvoiceInfo, IHyInvoiceInfoService> {
	@Autowired
	private IHyInvoiceInfoService hyInvoiceInfoService;
	 @Autowired
	 private UserUtil userUtil;

	/**
	 * 分页列表查询
	 *
	 * @param hyInvoiceInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="开票信息管理-分页列表查询", notes="开票信息管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyInvoiceInfo hyInvoiceInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyInvoiceInfo> queryWrapper = QueryGenerator.initQueryWrapper(hyInvoiceInfo, req.getParameterMap());
		if (userUtil.isCustomerAdmin()) {
			queryWrapper.in("owner_id", userUtil.getCustomerAdminRelationIdList());
		}

		Page<HyInvoiceInfo> page = new Page<HyInvoiceInfo>(pageNo, pageSize);
		IPage<HyInvoiceInfo> pageList = hyInvoiceInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   分公司添加
	 *
	 * @param hyInvoiceInfo
	 * @return
	 */
	@AutoLog(value = "开票信息管理-分公司添加")
	@ApiOperation(value="开票信息管理-分公司添加", notes="开票信息管理-分公司添加")
	@PostMapping(value = "/addForCompany")
	public Result<?> addForCompany(@RequestBody HyInvoiceInfo hyInvoiceInfo) {
		HyInvoiceInfo invoiceInfo = hyInvoiceInfoService.getByOwnerId(hyInvoiceInfo.getOwnerId());
		if(invoiceInfo != null) {
			return Result.error("此分公司已添加过开票信息，不可重复添加！");
		}
		hyInvoiceInfoService.save(hyInvoiceInfo);
		return Result.OK("添加成功！");
	}
	 /**
	  *   货主添加
	  *
	  * @param hyInvoiceInfo
	  * @return
	  */
	 @AutoLog(value = "开票信息管理-货主添加")
	 @ApiOperation(value="开票信息管理-货主添加", notes="开票信息管理-货主添加")
	 @PostMapping(value = "/addForCustomer")
	 public Result<?> addForCustomer(@RequestBody HyInvoiceInfo hyInvoiceInfo) {
		 HyCustomer customer = userUtil.getCustomer();
		 if (customer == null) {
			 return Result.error("货主信息不存在");
		 }
		 HyInvoiceInfo old = hyInvoiceInfoService.getByOwnerId(customer.getId());
		 if(old != null){
			 return Result.error("不能重复添加");
		 }
		 hyInvoiceInfo.setOwnerId(customer.getId());
		 hyInvoiceInfoService.save(hyInvoiceInfo);
		 return Result.OK("添加成功！");
	 }

	 @AutoLog(value = "开票信息管理-货主添加")
	 @ApiOperation(value="开票信息管理-货主添加", notes="开票信息管理-货主添加")
	 @PostMapping(value = "/add")
	 public Result<?> add(@RequestBody HyInvoiceInfo hyInvoiceInfo) {
		 String ownerId = null;
		 if(userUtil.isSys()){
			 ownerId = hyInvoiceInfo.getOwnerId();
		 }else {
			 HyCustomer customer = userUtil.getCustomer();
			 if (customer == null) {
				 return Result.error("货主信息不存在");
			 }
			 ownerId = customer.getId();
			 hyInvoiceInfo.setOwnerId(ownerId);

		 }
		 if(StringUtils.isEmpty(ownerId)){
			 return Result.error("货主不能为空");
		 }
		 HyInvoiceInfo old = hyInvoiceInfoService.getByOwnerId(ownerId);
		 if(old != null){
			 return Result.error("不能重复添加");
		 }
		 hyInvoiceInfoService.save(hyInvoiceInfo);
		 return Result.OK("添加成功！");
	 }

	/**
	 *  编辑
	 *
	 * @param hyInvoiceInfo
	 * @return
	 */
	@AutoLog(value = "开票信息管理-编辑")
	@ApiOperation(value="开票信息管理-编辑", notes="开票信息管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyInvoiceInfo hyInvoiceInfo) {
		String id = hyInvoiceInfo.getId();
		if(StringUtils.isEmpty(id)){
			return Result.error("数据不存在");
		}
		HyInvoiceInfo old = hyInvoiceInfoService.getById(id);
		if(old == null){
			return Result.error("数据不存在");
		}
		if(!StringUtils.equals(old.getOwnerId() , hyInvoiceInfo.getOwnerId())){
			return Result.error("不能修改所属货主信息");
		}
		if(!userUtil.isSys()){
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				return Result.error("货主信息不存在");
			}
			if(!StringUtils.equals(customer.getId(), hyInvoiceInfo.getOwnerId())){
				return Result.error("货主信息错误");
			}
		}
		hyInvoiceInfoService.updateById(hyInvoiceInfo);
		return Result.OK("编辑成功!");
	}


	/**
	 * 查询货主开票信息
	 *
	 * @return
	 */
//	@AutoLog(value = "开票信息管理-查询货主开票信息")
	@ApiOperation(value="开票信息管理-查询货主开票信息", notes="开票信息管理-查询货主开票信息")
	@GetMapping(value = "/queryInfo")
	public Result<?> queryInfo() {
		HyCustomer customer = userUtil.getCustomer();
		if (customer == null) {
			return Result.error("货主信息不存在");
		}
		HyInvoiceInfo hyInvoiceInfo = hyInvoiceInfoService.getByOwnerId(customer.getId());
		if(hyInvoiceInfo==null) {
			return Result.error("尚未填写开票信息，请到“开票管理-开票信息设置”中填写！");
		}
		return Result.OK(hyInvoiceInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyInvoiceInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyInvoiceInfo hyInvoiceInfo) {
        return super.exportXls(request, hyInvoiceInfo, HyInvoiceInfo.class, "开票信息管理");
    }

}
